<?php
namespace App\Http\Controllers\admin_system;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\V1\Active666Model;
use App\Common\MobileMassege;
use DB;
/**
 * Class Activity666Controller.
 *
 * @property \App\Models\Active666Model             $activeModel
 *
 */
class Zhongchou666Controller extends Controller{

    public $activeModel;

    public function enter(Request $request)
    {
        $this->activeModel  = new Active666Model();
        $function           = $request->input('fun');
        return $this->$function($request);
    }


    /**
     * 管理员列表
     * */
    public function managers(Request $request)
    {
        $managers = $this->activeModel->get_managers();
        return view('/admin_system/zhongchou666/managers',['managers'=>$managers]);
    }


    public function delete_manager(Request $request)
    {
        $managerId  = $request->input('managerId');
        $this->activeModel->delete_manager($managerId);
        return back();
    }


    /**
     * 编辑管理员
     * */
    public function edit_manager(Request $request)
    {
        $managerId      = $request->input('managerId');

        $managers       = $this->activeModel->get_managers_by_type(1);
        if($managerId > 0)
        {
            $managerInfo    = $this->activeModel->get_manager_info($managerId);
        }else{
            $managerInfo    = new \stdClass();
            $managerInfo->id = 0;
            $managerInfo->real_name = "";
            $managerInfo->mobile    = "";
            $managerInfo->bank_name = "";
            $managerInfo->bank_code = "";
        }

        return view('/admin_system/zhongchou666/edit-manager',['manager'=>$managerInfo]);
    }


    /**
     * 编辑管理员操作
     * */
    public function edit_manager_action(Request $request)
    {
        $managerId  = $request->input('managerId');
        $data   = [
            'real_name' => $request->input('real_name'),
            'mobile'    => $request->input('mobile'),
            'bank_name' => $request->input('bank_name'),
            'bank_code' => $request->input('bank_code')
        ];

        if($managerId > 0)  //修改
        {
            $this->activeModel->update_manager($managerId,$data);
        }
        else                //添加
        {
            //检查手机号是否使用
            $info               = $this->activeModel->get_manager_info_by_mobile($data['mobile']);
            if($info)
            {
                return apiData()->notice('已存在'.$data['mobile']."这个账号");
            }
            $pass               = rand(100000,999999);
            $data['password']   = md5($pass);
            $this->activeModel->add_manager($data);
            $msg                = "已经为您添加管理员身份,请使用账号:".$data['mobile'].",密码:".$pass."进行登录";
            $mobileMessage      = new MobileMassege();
            $mobileMessage->mobile  = $data['mobile'];
            $mobileMessage->send_msg($msg);
        }
        return $this->managers($request);
    }


    /*
     * 订单列表
     * */
    public function orders(Request $request)
    {
        $managerId  = $request->input('managerId',0);
        $isSuccess  = $request->input('isSuccess',-1);

        $orders = $this->activeModel->get_orders($managerId,$isSuccess);
        return view('/admin_system/zhongchou666/orders',['orders'=>$orders,'isSuccess'=>$isSuccess]);
    }

    /**
     * 删除订单
     * */
    public function delete_order(Request $request)
    {
        $orderId    = $request->input('orderId');
        DB::table('666_order')->where('id',$orderId)
            ->update(['deleted_at'=>date_time()]);
        return back();
    }
}